const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: '', // 订单id
    item_id: '', // 项目Id
    icon: '', // 项目图标
    item_name: '', // 项目名称
    processDescription: '', // 验收描述
    questions: [], // 问题点
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,
      is_login_inspector = wx.getStorageSync('is_login_inspector');
    if (!is_login_inspector || is_login_inspector != 1) {
      wx.showToast({
        title: '请先登陆验房师账号',
        icon: 'none',
        duration: 1500
      })
      wx.navigateBack({
        delta: 0,
      })
      return false;
    }
    if (options) {
      const {item_id, icon, item_name, order_id} = options
      that.setData({
        item_id,
        icon,
        item_name,
        order_id
      })
      this.getBookOrderItemInfo(this.data.item_id)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 获取验房项目详细
   * @param item_id 项目id
   */
  getBookOrderItemInfo(item_id) {
    wx.request({
      url: 'https://wx.amagich.com/' + 'v1/' + 'getBookOrderItemInfo',
      data: {
        order_item_id: item_id,
      },
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      success: res => {
        const {checkLogs, processDescription, name: item_name, icon} = res.data.resultData;
        this.setData({
          processDescription,
          item_name,
          icon,
          questions: checkLogs.map(item => {
            return {
              desc: item.note,
              pics: item.pics || [],
              id: item.id
            }
          })
        })
      }})
  },

  // 增加问题
  addQuestion() {
    const questions = JSON.parse(JSON.stringify(this.data.questions))
    questions.push({
      desc: '',
      pics: []
    })
    this.setData({
      questions
    })
  },
  // 移除问题
  removeQuestion(event) {
    const index = event.target.dataset.index
    const questions = JSON.parse(JSON.stringify(this.data.questions))
    console.log(questions)
    questions.splice(index,1)
    console.log('removeQuestion', questions)
    this.setData({
      questions
    })
  },
  // 备注
  confirmRemark(event) {
    const newVal = event.detail.value; // 新的值
    const index = event.target.dataset.index
    const questions = JSON.parse(JSON.stringify(this.data.questions))
    questions[index].desc = newVal
    this.setData({
      questions
    })
  },
  // 上传图片
  uploadImg: function (e) {
    var that = this;
    const {index} = e.target.dataset
    console.log('uploadImg index', index)
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log('res', res)
        const tempFilePaths = res.tempFilePaths[0];
        wx.uploadFile({
          url: https.url + '/upload',
          filePath: tempFilePaths,
          name: 'file',
          success: (resp) => {
            const url = JSON.parse(resp.data)
            const questions = JSON.parse(JSON.stringify(that.data.questions))
            questions[index].pics.push(url)
            that.setData({
              questions
            })
          },
          fail: (res) => {

          },
        });
      }
    })
  },
  // 保存
  save: function () {
    const inspector_id = wx.getStorageSync('inspector_id');
    const order_item_id = this.data.item_id;
    let result = null;
    if(this.data.questions.length > 0) {
      result = JSON.parse(JSON.stringify(this.data.questions))
      result = result.map(item => {
        return {
          note: item.desc,
          pics: item.pics.join(',')
        }
      })
    }
    https.post('saveBookOrderItem',{inspector_id, order_item_id, result, check_result: this.data.questions.length > 0 ? 0: 1 },false,'提交中').then(res=>{
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000
      })
      wx.navigateBack({delta: 1})
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})
